﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Lesson03
{
    public partial class _Default : System.Web.UI.Page
    {
        private const string CACHE_FILE = "FileCache";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Cache["Greeting"] = "Hello cache.";
            if (this.Cache["Greeting"] != null)
                this.Label1.Text = this.Cache["Greeting"].ToString();
            else
                this.Label1.Text = "Hello world!";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //cache dependency
            this.Cache.Insert(CACHE_FILE, File.ReadAllText(Server.MapPath("SourceFile.txt")),
                        new System.Web.Caching.CacheDependency(Server.MapPath("SourceFile.txt")));
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (this.Cache[CACHE_FILE] != null)
                this.txtResult.Text = this.Cache[CACHE_FILE].ToString();
            else
                this.txtResult.Text = string.Empty;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //absolute cache expiration
            this.Cache.Insert(CACHE_FILE, "Cach file with 1 minute", null, DateTime.Now.AddMinutes(1),
                            System.Web.Caching.Cache.NoSlidingExpiration);
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (this.Cache[CACHE_FILE] != null)
                this.txtResult.Text = this.Cache[CACHE_FILE].ToString();
            else
                this.txtResult.Text = string.Empty;
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            //timespan cache expiration
            this.Cache.Insert(CACHE_FILE, "Cache content with 1 minute timespan", null, System.Web.Caching.Cache.NoAbsoluteExpiration,
                                new TimeSpan(0, 1, 0));
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if (this.Cache[CACHE_FILE] != null)
                this.txtResult.Text = this.Cache[CACHE_FILE].ToString();
            else
                this.txtResult.Text = string.Empty;
        }

        protected void btnSubmitButton_Click(object sender, EventArgs e)
        {
            this.lblChosenLabel.Text = string.Format("{0} at {1}",
                                       this.ddlChoiceDropDownList.SelectedValue,
                                       System.DateTime.Now.TimeOfDay.ToString());
        }
    }
}
